import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:get/get.dart';
import 'package:get/get_navigation/src/root/get_material_app.dart';
import 'package:wanandroid_flutter/pages/home_page.dart';
import 'package:wanandroid_flutter/pages/mine_page.dart';
import 'package:wanandroid_flutter/pages/project_page.dart';
import 'package:wanandroid_flutter/pages/system_page.dart';
import 'package:wanandroid_flutter/utils/error_handle.dart';
import 'package:wanandroid_flutter/utils/user.dart';

import 'constants/constants.dart';
import 'network/request_util.dart';

void main() {
  handleError(() async {
    WidgetsFlutterBinding.ensureInitialized();
    // 初始化dio
    configDio(baseUrl: Constant.baseUrl);

    EasyRefresh.defaultHeaderBuilder = () => const ClassicHeader(
        processingText: "正在刷新...",
        readyText: "正在刷新...",
        armedText: "释放以刷新",
        dragText: "下拉刷新",
        processedText: "刷新成功",
        failedText: "刷新失败",
        messageText: "上次刷新 %T");
    EasyRefresh.defaultFooterBuilder = () => const ClassicFooter(
        processingText: "加载中...",
        processedText: "加载成功",
        readyText: "加载中...",
        armedText: "释放以加载更多",
        dragText: "上拉加载",
        failedText: "加载失败",
        noMoreText: "没有更多内容",
        messageText: "上次加载 %T",
        position: IndicatorPosition.behind,
        showMessage: false);

    runApp(const MyApp());
  });
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return GetMaterialApp(
      builder: FToastBuilder(),
      debugShowCheckedModeBanner: false,
      title: '玩Android',
      theme: ThemeData(
        scaffoldBackgroundColor: Colors.grey.shade100,
        colorScheme: ColorScheme.fromSeed(
            primary: Colors.green.shade500, seedColor: Colors.green),
        useMaterial3: true,
      ),
      home: const MainPage(title: '玩Android'),
    );
  }
}

class MainPage extends StatefulWidget {
  const MainPage({super.key, required this.title});

  final String title;

  @override
  State<MainPage> createState() => _MainPageState();
}

class _MainPageState extends State<MainPage> {
  int _selectedItemIndex = 0;

  final PageController _pageController = PageController(initialPage: 0);

  final List<String> _titles = ["首页", "体系", "项目", "我的"];
  final List<Widget> _navIcons = [
    const Icon(Icons.home),
    const Icon(Icons.synagogue_sharp),
    const Icon(Icons.account_balance_wallet_rounded),
    const Icon(Icons.person)
  ];

  final List<Widget> _page = [
    HomePage(),
    SystemPage(),
    ProjectPage(),
    MinePage()
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        bottomNavigationBar: BottomNavigationBar(
            type: BottomNavigationBarType.fixed,
            selectedFontSize: 14,
            unselectedFontSize: 14,
            iconSize: 24,
            selectedItemColor: Theme.of(context).colorScheme.primary,
            unselectedItemColor: Theme.of(context).colorScheme.onSurface,
            items: _generateBottomNavList(),
            currentIndex: _selectedItemIndex,
            onTap: (index) {
              _pageController.jumpToPage(index);
            }),
        body: PageView.builder(
            physics: const NeverScrollableScrollPhysics(),
            itemBuilder: (context, index) {
              return _page[index];
            },
            onPageChanged: _onPageChanged,
            controller: _pageController));
  }

  List<BottomNavigationBarItem> _generateBottomNavList() {
    return List.generate(_titles.length, (index) {
      return BottomNavigationBarItem(
          icon: _navIcons[index], label: _titles[index]);
    });
  }

  void _onPageChanged(int index) {
    setState(() {
      _selectedItemIndex = index;
    });
  }
}
